package com.example.demo.task.notify;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiangjinwu
 * @version 1.0
 * @since 2025/10/10 1:24
 */

@Slf4j

public class NativeAudioPlayer {
  public static void playWav(String filePath) {
    try {
      // 1. 读取音频文件
      File audioFile = new File(filePath);
      AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

      // 2. 获取音频格式和混音器
      AudioFormat format = audioStream.getFormat();
      DataLine.Info info = new DataLine.Info(Clip.class, format);
      Clip clip = (Clip) AudioSystem.getLine(info);

      // 3. 加载并播放音频
      clip.open(audioStream);
      clip.start();

      // 等待播放完成（避免线程提前结束）
      Thread.sleep(clip.getMicrosecondLength() / 1000);

    } catch (UnsupportedAudioFileException | LineUnavailableException | IOException |
             InterruptedException e) {
      e.printStackTrace();
    }
  }
}
